package util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class DateUtil {

    public static final String PATTERN = "yyyy-MM-dd hh:mm:ss";
    public static final String YYYYMMDD = "yyyyMMdd";

    public static String getTodayStr(){
        return LocalDate.now().format(DateTimeFormatter.ofPattern(YYYYMMDD));
    }

    /**
     * @return 过去第二天零点(Date类型)
     */
    public static Date getTomorrow(){
        LocalDateTime of = LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.MIN);
        String tomorrowTime = of.format(DateTimeFormatter.ofPattern(PATTERN));
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(PATTERN);
        Date parse = null;
        try {
             parse = simpleDateFormat.parse(tomorrowTime);
        } catch (ParseException e) {
            log.error("日期解析出错{}",tomorrowTime);
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * @return 获取从当日开始的前面7天的 yyyyMMdd格式的日期戳集合
     */
    public static List<String>getDayStrList(){
        List<String>daysStr=new ArrayList<>(7);
        for (int i = 1; i <= 7; i++) {
            LocalDate preSeven = LocalDate.now().minusDays(i);
            daysStr.add(preSeven.format(DateTimeFormatter.ofPattern(YYYYMMDD)));
        }
        return daysStr;
    }

}
